package me.gacl.web.controller;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import me.gacl.domain.User;

/**
* @ClassName: Sendmail
* @Description: Sendmail类继承Thread，因此Sendmail就是一个线程类，这个线程类用于给指定的用户发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午10:43:48
*
*/ 
public class Sendmail extends Thread {
	
	static String mailHost = null;
	static String userName = null;
	static String password = null;
	static String userEmail = null;
	static {
		int type = 2;// 0 代表搜狐 1 代表新浪 2 代表163邮箱 默认是搜狐0
		if (type == 0) {
			mailHost = "smtp.sohu.com";
			userName = "zhuzj13387564856";
			password = "zzj614418.w.y.l2";
			password = "enpqNjE0NDE4LncueS5sMg==";
			userEmail = "zhuzj13387564856@sohu.com";
		} else if (type == 1) {
			mailHost = "smtp.sina.com";
			userName = "zhuzj13387564856";
			password = "zzj614418.w.y.l2";// enpqNjE0NDE4LncueS5sMg==
			// password = "enpqNjE0NDE4LncueS5sMg==";
			userEmail = "zhuzj13387564856@sina.com";
		}
		else if (type == 2) {
			mailHost = "smtp.163.com";
			userName = "13387564856@163.com";//这里用用户名和邮箱地址都可以
			password = "13387564856";
			userEmail = "13387564856@163.com";
		}
	}

	//用于给用户发送邮件的邮箱
	private String from = userEmail;
	//邮箱的用户名
	private String username = userName;
	//邮箱的密码
	private String pass = password;
	//发送邮件的服务器地址
	private String host = mailHost;
	
	private User user;
    public Sendmail(User user){
        this.user = user;
    }
    
    /* 重写run方法的实现，在run方法中发送邮件给指定的用户
     * @see java.lang.Thread#run()
     */
    @Override
    public void run() {
        try{
            Properties prop = new Properties();
            prop.setProperty("mail.host", host);
            prop.setProperty("mail.transport.protocol", "smtp");
            prop.setProperty("mail.smtp.auth", "true");
            Session session = Session.getInstance(prop);
            session.setDebug(true);
            Transport ts = session.getTransport();
            ts.connect(host, username, pass);
            Message message = createEmail(session,user,from);
            ts.sendMessage(message, message.getAllRecipients());
            ts.close();
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
    * @Method: createEmail
    * @Description: 创建要发送的邮件
    * @Anthor:孤傲苍狼
    *
    * @param session
    * @param user
    * @return
    * @throws Exception
    */ 
    public Message createEmail(Session session,User user,String from) throws Exception{
        
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
        message.setSubject("用户注册邮件");
        
        String info = "恭喜您注册成功，您的用户名：" + user.getUsername() + ",您的密码：" + user.getPassword() + "，请妥善保管，如有问题请联系网站客服!!";
        message.setContent(info, "text/html;charset=UTF-8");
        message.saveChanges();
        return message;
    }
	
}
